import env from '@/imageFactory/ENV'

const IMAGEVAL = function(file) { // element 图片大小判断
  const maxsize = 2 * 1024 * 1024// 2M
  const isLt2M = file.size
  if (isLt2M > maxsize) {
    return false
  } else {
    return true
  }
}

const NativeEVAL = function(file) { // 原生 图片大小判断
  var URL = window.URL || window.webkitURL
  const maxsize = 2 * 1024 * 1024// 2M
  console.log(e.target.files[0])
  var imgURL = URL.createObjectURL(e.target.files[0])
  var img_url = e.target
  const isLt2M = img_url.files[0].size
  if (isLt2M > maxsize) {
    return false
  } else {
    return true
  }
}

const resflag = function(flag) {
  switch (flag) {
    case 422:
      // return
      break
    case 500:
      break
  }
}

const ImageHandlefunction = function(data) {
  const reg = {}
  data.forEach(function(item) {
    const res = item.split(/[0-9]+\/(.+)\//)
    if (!reg[res[1]]) {
      reg[res[1]] = []
    }
    reg[res[1]].push(env.UPLOADIMG + item)
  })
  return reg
}

var nueTime = function (time1, time2) {
  // 获取时间
  var start = time1;
  var End = time2;
  var statea = start.getMonth() + 1;
  var starttime = start.getFullYear() + '-' + (this.Appendzero(statea)) + '-' + this.Appendzero(start.getDate()) + ' ' + '00' + ':' + '00' + ':' + '00';
  var endstate = End.getMonth() + 1;
  var Endtime = End.getFullYear() + '-' + (this.Appendzero(endstate)) + '-' + this.Appendzero(End.getDate()) + ' ' + 23 + ':' + 59 + ':' + '59';
  return [starttime, Endtime];
}
var nueDay = function (time1, time2) {
  // 获取时间
  var start = time1;
  var End = time2;
  var statea = start.getMonth() + 1;
  var starttime = start.getFullYear() + '-' + (this.Appendzero(statea)) + '-' + this.Appendzero(start.getDate());
  var endstate = End.getMonth() + 1;
  var Endtime = End.getFullYear() + '-' + (this.Appendzero(endstate)) + '-' + this.Appendzero(End.getDate());
  return [starttime, Endtime];
}
var Appendzero = function (obj) {
  ////  日期 不够两位数 就在前面加一个0
  if (obj < 10) return "0" + "" + obj;
  else return obj;
};
let GetAge = function (strBirthday){     
    var returnAge,
        strBirthdayArr=strBirthday.split("-"),
        birthYear = strBirthdayArr[0],
        birthMonth = strBirthdayArr[1],
        birthDay = strBirthdayArr[2],  
        d = new Date(),
        nowYear = d.getFullYear(),
        nowMonth = d.getMonth() + 1,
        nowDay = d.getDate();   
    if(nowYear == birthYear){
        returnAge = 0;       //同年 则为0周岁
    }
    else{
        var ageDiff = nowYear - birthYear ; //年之差
        if(ageDiff > 0){
            if(nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay;//日之差
                if(dayDiff < 0) {
                    returnAge = ageDiff - 1;
                }else {
                    returnAge = ageDiff;
                }
            }else {
                var monthDiff = nowMonth - birthMonth;//月之差
                if(monthDiff < 0) {
                    returnAge = ageDiff - 1;
                }
                else {
                    returnAge = ageDiff ;
                }
            }
        }else {
            returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
        }
    } 
    return returnAge;//返回周岁年龄
}
export default {
  IMAGEVAL, // 图片大小判断
  NativeEVAL, // 原生判断图片大小
  ImageHandlefunction,// 图片处理
  Appendzero, // 日期 不够两位数 就在前面加一个0
  nueTime,  // 获取时间
  nueDay,  //没得时分秒
  GetAge, //根据时间获取年龄
}
